﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using Model;
using BLL;

namespace NCRE学生考试端V1._0.教师端操作类
{
    public partial class frmAddExamQuestion : Form
    {
        #region 单例模式，窗体单一显示
        private static frmAddExamQuestion frmaddexamquestion = null;
        private frmAddExamQuestion()
        {
            InitializeComponent();
        }
        public static frmAddExamQuestion GetInstance()
        {
            if (frmaddexamquestion == null || frmaddexamquestion.IsDisposed)
            {
                frmaddexamquestion = new frmAddExamQuestion();
            }
            return frmaddexamquestion;
        }
        #endregion

        #region 窗体加载时的设置
        private void frmAddExamQuestion_Load(object sender, EventArgs e)
        {
            //状态下拉框赋值
            if (comboxQuestionType.Items.Count == 0)
            {
                string[] listContent = { "选择题", "Windows题", "Word题", "PPT题", "Excel题", "IE题" };
                comboxQuestionType.Items.AddRange(listContent);
                comboxQuestionType.SelectedIndex = 0;
            }

            txtFileName.Text = "请选择文件位置：";
            dataGridView1.DataSource = "";

            lblWarnInfo.Visible = false;
        }


        #endregion

        #region 下载模板事件
        private void lklLoadModelFile_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (comboxQuestionType.Text.Trim () == "")
            {
                lblWarnInfo.Visible = true;
            }
            else
            {
                switch (comboxQuestionType.Text.Trim())
                {
                    case "选择题":
                        DownLoadSelectQuestionTemplate();
                        break;
                    case "Windows题":
                        DownLoadWinQuestionTemplate();
                        break;
                    case "Word题":
                        DownLoadWordQuestionTemplate();
                        break;
                    case "PPT题":
                        DownLoadPPTQuestionTemplate();
                        break;
                    case "Excel题":
                        DownLoadExcelQuestionTemplate();
                        break;
                    case "IE题":
                        DownLoadIEQuestionTemplate();
                        break;
                }      
            }

        }
        #endregion

        #region 下载各个题型模板的方法
        #region 下载选择题模板的方法
        private void DownLoadSelectQuestionTemplate()
        {
            List<string> columnListOut = new List<string>()
        {
            "题号",
            "题目",
            "选项A",
            "选项B",
            "选项C",
            "选项D",
            "标准答案"
        };
            string modelFileName = "选择题配置模板";
            string SheetName = "选择题信息";
            string[] columnList = columnListOut.ToArray();
            string msg = string.Empty;
            DownloadTemplate.SaveExcelTemplate(columnList, modelFileName, SheetName, ref msg);
        }
        #endregion

        #region 下载Windows题模板的方法
        private void DownLoadWinQuestionTemplate()
        {
            List<string> columnListOut = new List<string>()
        {
            "题号",
            "题目",
            "标准答案",
            "分值",
            "题目类型标志",
            "试卷类型"
        };
            string modelFileName = "Windows试题配置模板";
            string SheetName = "Windows试题";
            string[] columnList = columnListOut.ToArray();
            string msg = string.Empty;
            DownloadTemplate.SaveExcelTemplate(columnList, modelFileName, SheetName, ref msg);
        }
        #endregion

        #region 下载Word题模板的方法
        private void DownLoadWordQuestionTemplate()
        {
            /// <summary>
            /// 导出模板列集合
            /// </summary>
            List<string> columnListOut = new List<string>()
        {
            "题号",
            "分值",
            "试卷类型",
            "题目类型标志",
            "题目",
            "标准答案",
            "判分标志"
        };

            /// <summary>
            /// 导出模板文件名称
            /// </summary>
            string FileName = "Word操作题模板";

            /// <summary>
            /// Excel底层页签名称
            /// </summary>
            string SheetName = "Word操作题";
            string[] columnList = columnListOut.ToArray();
            string msg = string.Empty;
            DownloadTemplate.SaveExcelTemplate(columnList, FileName, SheetName, ref msg);
        }
        #endregion

        #region 下载PPT题模板的方法
        private void DownLoadPPTQuestionTemplate()
        {
            /// <summary>
            /// 导出模板列集合
            /// </summary>
            List<string> columnListOut = new List<string>()
            {
                "题号",
                "题目类型",
                "试卷类型",
                "分数",
                "题目标记",
                "题干",
                "正确答案",
                "参数坐标"
            };
            /// <summary>
            /// 导出模板文件名称
            /// </summary>
            string FileName = "Ppt配题模板";
            string SheetName = "PPt题信息";
            string[] columnList = columnListOut.ToArray();
            string msg = string.Empty;
            DownloadTemplate.SaveExcelTemplate(columnList, FileName, SheetName, ref msg);
        }
        #endregion

        #region 下载Excel题模板的方法
        private void DownLoadExcelQuestionTemplate()
        {
            /// <summary>
            /// 导出模板列集合
            /// </summary>
            List<string> columnListOut = new List<string>()
            {
                "序列号",
                "试题类型编号",
                "分数",
                "试卷类型",
                "试题类型",
                "题干",
                "工作表",
                "单元格",
                "正确答案",
                "是否使用"
            };
            string FileName = "Excel配题模板";
            string SheetName = "Excel题信息";
            string[] columnList = columnListOut.ToArray();
            string msg = string.Empty;
            DownloadTemplate.SaveExcelTemplate(columnList, FileName, SheetName, ref msg);
        }
        #endregion     

        #region 下载IE题模板的方法
        private void DownLoadIEQuestionTemplate()
        {
            /// <summary>
            /// 导出模板列集合
            /// </summary>
            List<string> columnListOut = new List<string>()
        {
            "题号",
            "分值",
            "试卷类型",
            "题目类型标志",
            "题目",
            "标准答案"
        };

            /// <summary>
            /// 导出模板文件名称
            /// </summary>
            string FileName = "IE操作题模板";

            /// <summary>
            /// Excel底层页签名称
            /// </summary>
            string SheetName = "IE操作题";
            string[] columnList = columnListOut.ToArray();
            string msg = string.Empty;
            DownloadTemplate.SaveExcelTemplate(columnList, FileName, SheetName, ref msg);

        }
        #endregion
        #endregion

        #region 浏览打开模板文件
        private void lblOpenFile_Click(object sender, EventArgs e)
        {
            try
            {
                if (comboxQuestionType.Text.Trim() == "")
                {
                    lblWarnInfo.Visible = true;
                }
                else
                {
                    OpenFileDialog ofd = new OpenFileDialog();
                    if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        txtFileName.Text = ofd.FileName;
                        TemplateDataInfoToDataGridView(txtFileName.Text);
                    }
                }
            }
            catch
            {
                MessageBox.Show("您打开的不是" + comboxQuestionType.Text.Trim () + "模板文件，请重新选择文件~");
                dataGridView1.DataSource = "";
                txtFileName.Text = "请选择文件位置：";
            }
        }
        #endregion

        #region 将模板中的数据信息显示在DataGridView控件中
        DataSet ds = null;
        private void TemplateDataInfoToDataGridView(string filename)
        {
            
            string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties='Excel 12.0; HDR = YES; IMEX = 1'";

            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();
            string strExcel = "";
            OleDbDataAdapter myCommand = null;
            switch (comboxQuestionType.Text.Trim())
            {
                case "选择题":
                    strExcel = "select 题号,题目, 选项A, 选项B ,选项C ,选项D ,标准答案 from [选择题信息$]";
                    break;
                case "Windows题":
                    strExcel = "select 题号,题目,标准答案,分值,题目类型标志,试卷类型 from [Windows试题$]";
                    break;
                case "Word题":
                    strExcel = "select 题号,分值,试卷类型,题目类型标志,题目,标准答案,判分标志 from [Word操作题$]";
                    break;
                case "PPT题":
                    strExcel = "select 题号,题目类型,试卷类型,分数,题目标记,题干,正确答案,参数坐标 from [PPT题信息$]";
                    break;
                case "Excel题":
                    strExcel = "select 序列号,试题类型编号, 分数, 试卷类型 ,试题类型 ,题干 ,工作表 ,单元格 ,正确答案 ,是否使用 from [Excel题信息$]";
                    break;
                case "IE题":
                    strExcel = "select 题号,分值,试卷类型,题目类型标志,题目,标准答案 from [IE操作题$]";
                    break;
            }

            myCommand = new OleDbDataAdapter(strExcel, strConn);
            ds = new DataSet();
            myCommand.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0].DefaultView;
            dataGridView1.AllowUserToAddRows = false;
            //设定包括Header和所有单元格的列宽自动调整
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            //设定包括Header和所有单元格的行高自动调整
            dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;

        }
        #endregion

        #region 鼠标移到文件夹图标上时变为小手的形状
        private void lblOpenFile_MouseEnter(object sender, EventArgs e)
        {
            lblOpenFile.Cursor = Cursors.Hand;
        }
        #endregion

        #region 导入题库
        private void btnImport_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 0)
            {
                string strMsg = string.Empty;
                strMsg = "确定要提交导入吗？点击取消还可以在表格中继续修改哦~";
                DialogResult dr = MessageBox.Show(this, strMsg, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
                if (dr == DialogResult.OK)
                {
                    switch (comboxQuestionType.Text.Trim())
                    {
                        case "选择题":
                            ImportSelectQuestion();
                            break;
                        case "Windows题":
                            ImportWinQuestion();
                            break;
                        case "Word题":
                            ImportWordQuestion();
                            break;
                        case "PPT题":
                            ImportPPTQuestion();
                            break;
                        case "Excel题":
                            ImportExcelQuestion();
                            break;
                        case "IE题":
                            ImportIEQuestion();
                            break;
                    }
                }
            }
            else
            {
                MessageBox.Show("试题信息为空，请重新配置！");
                dataGridView1.DataSource = "";
                txtFileName.Text = "请选择文件位置：";
            }
        }
        #endregion


        #region 导入选择题的方法
        private void ImportSelectQuestion()
        {
            List<SelectQuestionEntity> lstSelectQuestion = new List<SelectQuestionEntity>();
            
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                SelectQuestionEntity enSelectQuestion = new SelectQuestionEntity();
                enSelectQuestion.QuestionContent = dataGridView1.Rows[i].Cells[1].Value.ToString();
                enSelectQuestion.OptionA = dataGridView1.Rows[i].Cells[2].Value.ToString();
                enSelectQuestion.OptionB = dataGridView1.Rows[i].Cells[3].Value.ToString();
                enSelectQuestion.OptionC = dataGridView1.Rows[i].Cells[4].Value.ToString();
                enSelectQuestion.OptionD = dataGridView1.Rows[i].Cells[5].Value.ToString();
                enSelectQuestion.RightAnswer = dataGridView1.Rows[i].Cells[6].Value.ToString();
                lstSelectQuestion.Add(enSelectQuestion);
            }
            SelectQuestionBll selectquestionbll = new SelectQuestionBll();
            int flag = selectquestionbll.InsertNewSelectQuestion(lstSelectQuestion);
            if (flag == lstSelectQuestion.Count)
            {
                MessageBox.Show("导入成功！");
                dataGridView1.DataSource = "";
                txtFileName.Text = "请选择文件位置：";
            }
            else
            {
                lblWarnInfo.Text = "导入成功" + flag + "条，表格中为未上传数据！";
                //返回错误数据标志，删除错误标志之前的数据                             
                for (int i = 0; i < flag; i++)
                {
                    dataGridView1.Rows.RemoveAt(0);
                }                
                          
                txtFileName.Text = "请选择文件位置：";
            }

        }
        #endregion

        #region 导入Windows题的方法
        private void ImportWinQuestion()
        {
            List<WinQuestionEntity> lstWinQuestion = new List<WinQuestionEntity>();
            
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                WinQuestionEntity enWinQuestion = new WinQuestionEntity();
                enWinQuestion.questionID = (double)(dataGridView1.Rows[i].Cells[0].Value);
                enWinQuestion.questionContent = dataGridView1.Rows[i].Cells[1].Value.ToString();
                enWinQuestion.correctAnswer = dataGridView1.Rows[i].Cells[2].Value.ToString();
                enWinQuestion.fraction = dataGridView1.Rows[i].Cells[3].Value.ToString();
                enWinQuestion.questionFlag = dataGridView1.Rows[i].Cells[4].Value.ToString();
                enWinQuestion.paperType = dataGridView1.Rows[i].Cells[5].Value.ToString();
                lstWinQuestion.Add(enWinQuestion);
            }
            WinQuestionEntityBll winquestionbll = new WinQuestionEntityBll();
            int flag = winquestionbll.InsertNewWinQuestion(lstWinQuestion);
            if (flag==lstWinQuestion.Count)
            {
                MessageBox.Show("导入成功！");
                dataGridView1.DataSource = "";
                txtFileName.Text = "请选择文件位置：";
            }
            else
            {
                lblWarnInfo.Text = "导入成功" + flag + "条，表格中为未上传数据！";
                //返回错误数据标志，删除错误标志之前的数据 
                for (int i = 0; i < flag; i++)
                {
                    dataGridView1.Rows.RemoveAt(0);
                }
                txtFileName.Text = "请选择文件位置：";
            }

        }
        #endregion

        #region 导入Word题的方法
        private void ImportWordQuestion()
        {
            List<WordQuestionEntity> wordquestion = new List<WordQuestionEntity>();

            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                WordQuestionEntity wordquestionentity = new WordQuestionEntity();
                wordquestionentity.Fration = dataGridView1.Rows[i].Cells[1].Value.ToString();
                wordquestionentity.QuestionFlag = dataGridView1.Rows[i].Cells[3].Value.ToString();
                wordquestionentity.PaperType = dataGridView1.Rows[i].Cells[2].Value.ToString();
                wordquestionentity.QuestionContent = dataGridView1.Rows[i].Cells[4].Value.ToString();
                wordquestionentity.RightAnswer = dataGridView1.Rows[i].Cells[5].Value.ToString();
                wordquestionentity.QuestionParameter = dataGridView1.Rows[i].Cells[6].Value.ToString();

                wordquestion.Add(wordquestionentity);
            }

            WordQuestionEntityBll wordquestionbll = new WordQuestionEntityBll();
            int flag = wordquestionbll.InsertWordQuestion(wordquestion);
            if (flag == wordquestion.Count)
            {
                MessageBox.Show("导入成功！");
                dataGridView1.DataSource = "";
                txtFileName.Text = "请选择文件位置：";
            }
            else
            {
                lblWarnInfo.Text = "导入成功" + flag + "条，表格中为未上传数据！";
                //返回错误数据标志，删除错误标志之前的数据
                for (int i = 0; i < flag; i++)
                {
                    dataGridView1.Rows.RemoveAt(0);
                }
                txtFileName.Text = "请选择文件位置：";
            }
        }
        #endregion

        #region 导入PPT题的方法
        private void ImportPPTQuestion()
        {
            if (dataGridView1.DataSource == null)
            {
                MessageBox.Show("请选择将要上传的文件！");
                return;
            }

            //将数据集中的数据转到dt表中
            DataTable dt = ds.Tables[0];
            PptQuestionEntityBLL pptEntitybll = new PptQuestionEntityBLL();

            //接收批量导入学生的返回值
            int flag = pptEntitybll.AddPptQuestion(dt);
            if (flag == dt.Rows.Count)
            {
                MessageBox.Show("导入成功！");
                dataGridView1.DataSource = "";
                txtFileName.Text = "请选择文件位置：";
            }
            else
            {
                lblWarnInfo.Text = "导入成功" + flag + "条，表格中为未上传数据！";
                //返回错误数据标志，删除错误标志之前的数据
                for (int i = 0; i < flag; i++)
                {
                    dataGridView1.Rows.RemoveAt(0);
                }
                txtFileName.Text = "请选择文件位置：";
            }
        }
        #endregion

        #region 导入Excel题的方法
        private void ImportExcelQuestion()
        {
            if (dataGridView1.DataSource == null)
            {
                MessageBox.Show("请选择将要上传的文件！");
                return;
            }
            //将数据集中的数据转到dt表中
            DataTable dt = ds.Tables[0];
            ExcelEntityBll excelEntitybll = new ExcelEntityBll();

            //接收批量导入学生的返回值
            int flag = excelEntitybll.AddExcelQuestion(dt);
            if (flag == dt.Rows.Count)
            {
                MessageBox.Show("导入成功！");
                dataGridView1.DataSource = "";
                txtFileName.Text = "请选择文件位置：";
            }
            else
            {
                lblWarnInfo.Text = "导入成功" + flag + "条，表格中为未上传数据！";
                //返回错误数据标志，删除错误标志之前的数据
                for (int i = 0; i < flag; i++)
                {
                    dataGridView1.Rows.RemoveAt(0);
                }
                txtFileName.Text = "请选择文件位置：";
            }
        }
        #endregion

        #region 导入IE题的方法
        private void ImportIEQuestion()
        {
            List<IEQuestionEntity> lstIEQuestion = new List<IEQuestionEntity>();
            
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                IEQuestionEntity enIEQuestion = new IEQuestionEntity();
                enIEQuestion.fraction = dataGridView1.Rows[i].Cells[1].Value.ToString();
                enIEQuestion.questionFlag = dataGridView1.Rows[i].Cells[3].Value.ToString();
                enIEQuestion.paperType = dataGridView1.Rows[i].Cells[2].Value.ToString();
                enIEQuestion.questionContent = dataGridView1.Rows[i].Cells[4].Value.ToString();
                enIEQuestion.correctAnswer = dataGridView1.Rows[i].Cells[5].Value.ToString();

                lstIEQuestion.Add(enIEQuestion);
            }
            IeQuestionEntityBll IEquestionbll = new IeQuestionEntityBll();
            int flag = IEquestionbll.InsertNewIEQuestion(lstIEQuestion);
            if (flag == lstIEQuestion.Count)
            {
                MessageBox.Show("导入成功！");
                dataGridView1.DataSource = "";
                txtFileName.Text = "请选择文件位置：";                
            }
            else
            {
                lblWarnInfo.Text = "导入成功" + flag + "条，表格中为未上传数据！";
                //返回错误数据标志，删除错误标志之前的数据
                for (int i = 0; i < flag; i++)
                {
                    dataGridView1.Rows.RemoveAt(0);
                }
                txtFileName.Text = "请选择文件位置：";
            }

        }
        #endregion

        private void comboxQuestionType_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataGridView1.DataSource = "";
            txtFileName.Text = "请选择文件位置：";
            lblWarnInfo.Visible = false;
        }
    }
}
